﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-20>
//创建作者：<郭登杰>
//功能说明：来函公共控件-计划变更申请信息
//********************************************************************************//
using BLL.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Entity;
using BLL;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHShenQingXX_JHBG : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }

        /// <summary>
        /// 应收金额
        /// </summary>
        public decimal YingShouJE
        {
            get
            {
                return ViewState["YingShouJE"] == null ? -1 : Convert.ToDecimal(ViewState["YingShouJE"]);
            }
            set
            {
                ViewState["YingShouJE"] = value;
            }
        }

        /// <summary>
        /// 实收金额
        /// </summary>
        public decimal ShiShouJE
        {
            get
            {
                return ViewState["ShiShouJE"] == null ? -1 : Convert.ToDecimal(ViewState["ShiShouJE"]);
            }
            set
            {
                ViewState["ShiShouJE"] = value;
            }
        }

        /// <summary>
        /// 加价金额
        /// </summary>
        public decimal JiaJiaJE
        {
            get
            {
                return ViewState["JiaJiaJE"] == null ? -1 : Convert.ToDecimal(ViewState["JiaJiaJE"]);
            }
            set
            {
                ViewState["JiaJiaJE"] = value;
            }
        }

        /// <summary>
        /// 用水价
        /// </summary>
        public decimal YSJ
        {
            get
            {
                return ViewState["ysj"] == null ? -1 : Convert.ToDecimal(ViewState["ysj"]);
            }
            set
            {
                ViewState["ysj"] = value;
            }
        }

        /// <summary>
        /// 加价倍数
        /// </summary>
        public int JJBS
        {
            get
            {
                return ViewState["jjbs"] == null ? -1 : Convert.ToInt32(ViewState["jjbs"]);
            }
            set
            {
                ViewState["jjbs"] = value;
            }
        }

        /// <summary>
        /// 费用编号
        /// </summary>
        public int FeiYongBH
        {
            get
            {
                return ViewState["FeiYongBH"] == null ? -1 : Convert.ToInt32(ViewState["FeiYongBH"]);
            }
            set
            {
                ViewState["FeiYongBH"] = value;
            }
        }

        /// <summary>
        /// 实用水量
        /// </summary>
        public int ShiYongSL
        {
            get
            {
                return ViewState["ShiYongSL"] == null ? -1 : Convert.ToInt32(ViewState["ShiYongSL"]);
            }
            set
            {
                ViewState["ShiYongSL"] = value;
            }
        }

        /// <summary>
        /// 实用水量
        /// </summary>
        public int JiaJiaYSL
        {
            get
            {
                return ViewState["JiaJiaYSL"] == null ? -1 : Convert.ToInt32(ViewState["JiaJiaYSL"]);
            }
            set
            {
                ViewState["JiaJiaYSL"] = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 响应客户端回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            //根据当前申请变更量计算核销金额及欠费金额
            int bgl = this.txtJHBGSQL.Text == "" ? 0 : int.Parse(this.txtJHBGSQL.Text);
            bgl=bgl-int.Parse(this.txtJHBGYJH.Text);
            //计算核销金额
            decimal hxje = bgl * YSJ * JJBS;
            hxje = (hxje > YingShouJE - ShiShouJE) ? YingShouJE - ShiShouJE : hxje;
            //计算变更后应收金额
            decimal ysje = YingShouJE - hxje;
            //计算变更后欠费金额
            decimal qfje = YingShouJE - hxje-ShiShouJE ;
            //e.Result = e.Parameter + "|0|" + hxje + "|" + qfje;
            e.Result = e.Parameter + "|0|" + hxje + "|" + ysje + "|" + qfje;
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            this.txtJHBGHXJE.ClientEnabled = false;
            this.txtJHBGJJJE.ClientEnabled = false;
            this.txtJHBGQFJE.ClientEnabled = false;
            this.txtJHBGSYL.ClientEnabled = false;
            this.txtJHBGYJH.ClientEnabled = false;
            this.txtJHBGYSJE.ClientEnabled = false;
            this.txtJHBGSQL.ClientEnabled = false;
            this.txtJHBGHXJE.ClientEnabled = false;           
            this.cbJHBGBLF.Visible = false;
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //先隐藏所有
            this.tdJHBGSQL.Visible = false;
            this.txtJHBGSQL.ClientEnabled = false;
            this.cbJHBGBLF.Visible = false;
            //根据来函显示面板
            //根据来函类型及当前流程显示意意见量
            if (this.LaiHanLC == eLaiHanLCBH.未登记 || this.LaiHanLC == eLaiHanLCBH.预登记)
            {
                this.tdJHBGSQL.Visible = true;
                this.txtJHBGSQL.ClientEnabled = true;                
                this.cbJHBGBLF.Visible = true;
            }
            else if (this.LaiHanLC == eLaiHanLCBH.登记)
            {
                this.tdJHBGSQL.Visible = true;
                this.txtJHBGSQL.ClientEnabled = false;
            }
            else if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.业务员)
            {
                this.tdJHBGSQL.Visible = false;
            }
        }

        /// <summary>
        /// 提交验证
        /// </summary>
        ValidateRS IUCLHOperate.Validate()
        {
            ValidateRS rs = new ValidateRS();
            if (this.txtJHBGYJH.Text == "")
            {
                rs.Msg = "请查询出计划信息。";
                return rs;
            }
            if (this.txtJHBGSQL.Text == "")
            {
                rs.Msg = "请输入变更量。";
                return rs;
            }
            //if (this.txtJHBGSQL.Text != this.txtJHBGSYL.Text)
            //{
            //    rs.Msg = "用户申请量与实用量不相等,请检查！";
            //    return rs;
            //}
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 判断是否需要变更
        /// </summary>
        /// <returns></returns>
        public ValidateRS ValidateNeedBG(YS_LH_LAIHANXX laihanxx)
        {
            ValidateRS rs = new ValidateRS();
            //判断是否未开账或无加价账单
            //获取实用量,计划量等
            /*
          int? syl_sy, yjh;
          DataRowCollection drcYSSJ = new SpHelper().P_YS_LH_YONGHUYSSJCX((int)laihanxx.I_Y, (int)laihanxx.I_M, laihanxx.S_YONGHUBH, out yjh, out syl_sy).Tables[0].Rows;
               }*/
            DataRowCollection drcYSSJ = new YS_JH_YONGHUYSSJProcess().SelectYS_JH_YONGHUYSSJsDynamic("I_JLZT=0,S_YONGHUH='" + laihanxx .S_YONGHUBH+ "',I_Y="+laihanxx.I_TZY+",I_M="+laihanxx.I_TZM, null).Rows;
            if (drcYSSJ.Count != 1)
            {
                rs.Msg = "无用水数据或有重复数据,请检查";
                return rs;
            }
              
            YS_ZW_YINGYEZ a = new YS_ZW_YINGYEZ();
            //获取对应年月营业账信息
            DataRowCollection drcYYZ = new YS_ZW_YINGYEZBLL().SelectYS_ZW_YINGYEZsDynamic("I_JLZT=0 and S_YONGHUH='" + laihanxx.S_YONGHUBH + "' and I_Y=" + drcYSSJ[0]["I_Y"] + " and I_M=" + drcYSSJ[0]["I_M"], null).Rows;
            if (drcYYZ.Count != 1)
            {
                rs.Msg = "该最近年月未开账或无加价账单";
                return rs;
            }
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtJHBGHXJE.Text = "";
            this.txtJHBGJJJE.Text = "";
            this.txtJHBGQFJE.Text = "";
            this.txtJHBGSQL.Text = "";
            this.txtJHBGSYL.Text = "";
            this.txtJHBGYJH.Text = "";
            this.txtJHBGYSJE.Text = "";
        }

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
            }
            //根据当前来函流程显示控件
            SetDisplay();
            if(laihanxx.S_YONGHUBH==null||laihanxx.I_Y==null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(laihanxx.S_LAIHANBH))
            {
                //加载申请变更量
                DataRowCollection drc = new SpHelper().P_YS_LHGETSHENPIYJS((int)laihanxx.I_LAIHANLX, laihanxx.S_LAIHANBH).Tables[0].Rows;
                List<int> indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int)laihanxx.I_LAIHANLX, eLaiHanLCBH.登记);
                if (indexs.Count == 0)
                {
                    indexs = LaiHanLCTools.GetNeedRowIndexs(drc, (int)laihanxx.I_LAIHANLX, eLaiHanLCBH.预登记);
                }
                if (indexs.Count != 1)
                {
                    if (laihanxx.I_LIUCHENGBH >= (int)eLaiHanLCBH.受理)
                    {
                        throw new Exception("不存在申请变更量步骤或申请变更量步骤重复,请检查");
                    }
                }
                else
                {
                    this.txtJHBGHXJE.Text = Convert.ToString(drc[indexs[0]]["N_HEXIAOJE"]);
                    this.txtJHBGJJJE.Text = Convert.ToString(drc[indexs[0]]["N_JIAJIAJE"]);
                    this.txtJHBGQFJE.Text = Convert.ToString(drc[indexs[0]]["N_QIANFEIJE"]);
                    this.txtJHBGSQL.Text = Convert.ToString(drc[indexs[0]]["I_SHENPIL"]);
                    this.txtJHBGSYL.Text = Convert.ToString(drc[indexs[0]]["I_SHIYONGSL"]);
                    this.txtJHBGYJH.Text = Convert.ToString(drc[indexs[0]]["I_YUANJIHUA"]);
                    this.txtJHBGYSJE.Text = Convert.ToString(drc[indexs[0]]["N_YINGSHOUJE"]);
                    this.YingShouJE = Convert.ToDecimal(drc[indexs[0]]["N_YINGSHOUJE"]);
                    this.ShiShouJE = Convert.ToDecimal(drc[indexs[0]]["N_SHISHOUJE"]);
                    this.YSJ = Convert.ToDecimal(drc[indexs[0]]["N_YSJ"]);
                    this.JJBS = Convert.ToInt32(drc[indexs[0]]["I_JIAJIACS"]);
                    this.FeiYongBH = Convert.ToInt32(drc[indexs[0]]["I_FEIYONGBH"]);
                    this.ShiYongSL = Convert.ToInt32(drc[indexs[0]]["I_SHIYONGSL"]);
                    this.JiaJiaYSL = Convert.ToInt32(drc[indexs[0]]["I_JIAJIAYSL"]);
                    this.JiaJiaJE = Convert.ToDecimal(drc[indexs[0]]["N_JIAJIAJE"]);
                }
            }
            else
            {                
                DataTable dt=new YS_JH_YONGHUYSSJProcess().SelectYS_JH_YONGHUYSSJsDynamic("I_Y=" + laihanxx.I_TZY + "AND I_M="+laihanxx.I_TZM+" AND S_YONGHUH='" + laihanxx.S_YONGHUBH + "'", null);
                this.txtJHBGSYL.Text = Convert.ToString(dt.Rows[0]["I_SHIYONGSL"]);
                this.txtJHBGYJH.Text = (Convert.ToInt32(dt.Rows[0]["I_YUANJIHUA"]) + Convert.ToInt32(dt.Rows[0]["I_CHUTIAOZ"]) + Convert.ToInt32(dt.Rows[0]["I_JIJIANJH"])).ToString();
                int jhl = Convert.ToInt32(dt.Rows[0]["I_YUANJIHUA"]);
                //DataRowCollection drcYSSJ = new SpHelper().P_YS_LH_YONGHUYSSJCX((int)laihanxx.I_TZY, (int)laihanxx.I_TZM, laihanxx.S_YONGHUBH, null).Tables[0].Rows;
                /*int jhl = 0;
                if (drcYSSJ.Count > 0)
                {
                    //实用量
                    this.txtJHBGSYL.Text = Convert.ToString(drcYSSJ[0]["I_SHIYONGSL"]);
                    //原计划
                    this.txtJHBGYJH.Text = Convert.ToString(drcYSSJ[0]["I_YUANJIHUA"]);
                    //计划量
                    jhl = Convert.ToInt32(drcYSSJ[0]["I_YUANJIHUA"]);
                }
                */
                //获取对应年月营业账信息
                DataRowCollection drcYYZ = new YS_ZW_YINGYEZBLL().SelectYS_ZW_YINGYEZsDynamic("I_JLZT=0 and S_YONGHUH='" + laihanxx.S_YONGHUBH + "' and I_Y=" + laihanxx.I_TZY + " and I_M=" + laihanxx.I_TZM, null).Rows;
                if (drcYYZ.Count > 0)
                {
                    
                    this.YingShouJE = Convert.ToDecimal(drcYYZ[0]["N_YINGSHOUJE"]);
                    //加价金额
                    this.txtJHBGJJJE.Text = Convert.ToDecimal(drcYYZ[0]["N_JIAJIAJE"]).ToString("0.00");
                    //实收金额
                    this.ShiShouJE = Convert.ToDecimal(drcYYZ[0]["N_SHISHOUJE"]);                    
                    //用水价
                    this.YSJ = Convert.ToDecimal(drcYYZ[0]["N_YSJ"]);
                    //加价倍数
                    this.JJBS = Convert.ToInt32(drcYYZ[0]["I_JIAJIACS"]);
                    //计算变更量
                    int sqbgl = (int)((YingShouJE - ShiShouJE) / JJBS / YSJ);
                    //重新计算申请量
                    int sql = sqbgl + jhl;
                    this.txtJHBGSQL.Text = sql.ToString();
                    // 申请量等于实用量即可
                    this.txtJHBGSQL.Text = drcYYZ[0]["i_shiyongsl"].ToString();
                    //重新计算核销金额
                    decimal hxje = (sqbgl * YSJ * JJBS);
                    hxje = (hxje > this.YingShouJE - this.ShiShouJE) ? this.YingShouJE - this.ShiShouJE : hxje;
                    this.txtJHBGHXJE.Text = hxje.ToString("0.00");
                    //重新计算应收金额
                    this.txtJHBGYSJE.Text = (this.YingShouJE-hxje).ToString();
                    //重新计算欠费金额
                    this.txtJHBGQFJE.Text = (this.YingShouJE - this.ShiShouJE-hxje).ToString("0.00");
                    //记录当前营业账
                    this.FeiYongBH = Convert.ToInt32(drcYYZ[0]["I_FEIYONGBH"]);
                    this.ShiYongSL = Convert.ToInt32(drcYYZ[0]["I_SHIYONGSL"]);
                    this.JiaJiaYSL = Convert.ToInt32(drcYYZ[0]["I_JIAJIAYSL"]);
                    this.JiaJiaJE = Convert.ToDecimal(drcYYZ[0]["N_JIAJIAJE"]);
                }
            }
        }

        /// <summary>
        /// 获取申请量
        /// </summary>
        /// <returns></returns>
        public object GetSQLS()
        {
            if (this.LaiHanLX == eLaiHanLX.计划变更)
            {
                YS_LH_JIHUABG obj = new YS_LH_JIHUABG();
                obj.I_SHENPIL = decimal.Parse(this.txtJHBGSQL.Text);
                obj.I_FEIYONGBH = this.FeiYongBH;
                obj.I_JIAJIACS = this.JJBS;
                obj.I_JIAJIAYSL = this.JiaJiaYSL;
                obj.I_SHIYONGSL = this.ShiYongSL;
                obj.N_SHISHOUJE = Convert.ToDouble(this.ShiShouJE);
                obj.N_YINGSHOUJE = Convert.ToDouble(this.txtJHBGYSJE.Text);
                obj.N_YSJ = Convert.ToDouble(this.YSJ);
                obj.N_JIAJIAJE = Convert.ToDouble(this.JiaJiaJE);
                obj.I_JIAJIACS = this.JJBS;
                obj.I_JIAJIAYSL = this.JiaJiaYSL;
                obj.I_SHIYONGSL = this.ShiYongSL;
                obj.N_SHISHOUJE = Convert.ToDouble(this.ShiShouJE);
                obj.N_YSJ = Convert.ToDouble(this.YSJ);
                obj.N_JIAJIAJE = Convert.ToDouble(this.JiaJiaJE);
                obj.I_ZWYM = BusinessTool.GetZWYM();
                obj.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                obj.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                obj.N_HEXIAOJE = double.Parse(this.txtJHBGHXJE.Text);
                obj.I_YUANJIHUA = Convert.ToInt32(this.txtJHBGYJH.Text);
                obj.N_QIANFEIJE = Convert.ToDouble(this.txtJHBGQFJE.Text);
                return obj;
            }
            else if (this.LaiHanLX == eLaiHanLX.表计因素注销)
            {
                YS_LH_BIAOJIYSZX obj = new YS_LH_BIAOJIYSZX();
                obj.I_SHENPIL = decimal.Parse(this.txtJHBGSQL.Text);
                obj.I_FEIYONGBH = this.FeiYongBH;
                obj.I_JIAJIACS = this.JJBS;
                obj.I_JIAJIAYSL = this.JiaJiaYSL;
                obj.I_SHIYONGSL = this.ShiYongSL;
                obj.N_SHISHOUJE = Convert.ToDouble(this.ShiShouJE);
                obj.N_YINGSHOUJE = Convert.ToDouble(this.YingShouJE);
                obj.N_YSJ = Convert.ToDouble(this.YSJ);
                obj.N_JIAJIAJE = Convert.ToDouble(this.JiaJiaJE);
                obj.I_JIAJIACS = this.JJBS;
                obj.I_JIAJIAYSL = this.JiaJiaYSL;
                obj.I_SHIYONGSL = this.ShiYongSL;
                obj.N_SHISHOUJE = Convert.ToDouble(this.ShiShouJE);
                obj.N_YINGSHOUJE = Convert.ToDouble(this.YingShouJE);
                obj.N_YSJ = Convert.ToDouble(this.YSJ);
                obj.N_JIAJIAJE = Convert.ToDouble(this.JiaJiaJE);
                obj.I_ZWYM = BusinessTool.GetZWYM();
                obj.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                obj.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                obj.N_HEXIAOJE = double.Parse(this.txtJHBGHXJE.Text);
                obj.I_YUANJIHUA = Convert.ToInt32(this.txtJHBGYJH.Text);
                obj.N_QIANFEIJE = Convert.ToDouble(this.txtJHBGQFJE.Text);
                return obj;
            }
            else
            {
                return null;
            }
        }

        #endregion
    }
}